
import { RuleState } from './types';
import type {
    FilterRuleOptions,
    RuleGenerationResult,
    RuleStyleConfig
} from './types';

export function generateRule(options: FilterRuleOptions): RuleGenerationResult {
    try {
        let result: RuleGenerationResult;
        switch (options.type) {
            case 'currency':
                result = generateCurrencyRule(options);
                break;
            case 'equipment':
                result = generateEquipmentRule(options);
                break;
            case 'jewel':
                result = generateJewelRule(options);
                break;
            case 'flask':
                result = generateFlaskRule(options);
                break;
            case 'skillgem':
                result = generateSkillGemRule(options);
                break;
            case 'unique':
                result = generateUniqueRule(options);
                break;
            case 'normalequipment':
                result = generateNormalEquipmentRule(options);
                break;
            case 'misc':
                result = generateMiscRule(options);
                break;
            case 'global':
                result = generateGlobalRule(options);
                break;
            default:
                throw new Error(`Unknown filter type: ${options.type}`);
        }

        // 检查规则生成结果
        if (!result.isValid) {
            console.warn(`Warning: Invalid rule generated for ${options.label}`, result.warnings);
            return {
                content: '',
                state: RuleState.Disabled,
                isValid: false,
                warnings: result.warnings
            };
        }

        // 如果规则被禁用，返回空内容
        if (result.state === RuleState.Disabled) {
            return {
                content: '',
                state: RuleState.Disabled,
                isValid: true
            };
        }

        return result;
    } catch (error) {
        console.error(`Error generating rule for ${options.label}:`, error);
        return {
            content: '',
            state: RuleState.Disabled,
            isValid: false,
            warnings: [error instanceof Error ? error.message : 'Unknown error']
        };
    }
}

// 导出类型和枚举
export type { FilterRuleOptions, RuleGenerationResult, RuleStyleConfig };
export { RuleState };

// 导入规则生成器
import { generateCurrencyRule } from './currencyRules';
import { generateEquipmentRule } from './equipmentRules';
import { generateJewelRule } from './jewelRules';
import { generateFlaskRule } from './flaskRules';
import { generateSkillGemRule } from './skillGemRules';
import { generateUniqueRule } from './uniqueRules';
import { generateNormalEquipmentRule } from './normalEquipmentRules';
import { generateMiscRule } from './miscRules';
import { generateGlobalRule } from './globalRules';